<?php
namespace alipay;
require_once '../../aop/AopClient.php';
require_once '../../aop/AopCertClient.php';
require_once '../../aop/AopCertification.php';
require_once '../../aop/AlipayConfig.php';
require_once '../../aop/request/AlipayFundTransUniTransferRequest.php';
require_once '../../aop/request/AlipayFundTransCommonQueryRequest.php';
class transfer{
    private $config;
    /**
     * transfer constructor.
     * @param $data
     * data{
     * privateKey:'支付宝私匙',
     * app_cert_path:'支付宝公匙路径',
     * alipay_cert_path:'支付宝',
     * alipay_root_cert_path：'支付宝根路径'，
     * appId：支付宝小程序appid
     * }
     */
    public function __construct($data)
    {
        $this->config = $data;
    }

    /**
     * @param $data
     * {'out_biz_no'    => time().rand(0,9),
    'remark'         => '转账测试',
    // 'business_params'=>['payer_show_name_use_alias'=>false],//设置别名
    'biz_scene'=>'DIRECT_TRANSFER',
    'payee_info' => [
    'identity' => '891873508@qq.com',//2088902548973973
    'identity_type' => 'ALIPAY_LOGON_ID',//ALIPAY_USER_ID
    'name'=>'吴建阳'
    ],
    'trans_amount'=>'0.1',
    'product_code'=>'TRANS_ACCOUNT_NO_PWD',
    'order_title'=>'转账测试'
     * }
     * @return bool|mixed|\SimpleXMLElement
     * @throws \Exception
     */
    public function execute($data){
        $alipayConfig = new AlipayConfig();
        $alipayConfig->setPrivateKey($this->config['privateKey']);
        $alipayConfig->setServerUrl("https://openapi.alipay.com/gateway.do");
        $alipayConfig->setAppId($this->config['appId']);
        $alipayConfig->setCharset("UTF-8");
        $alipayConfig->setSignType("RSA2");
        $alipayConfig->setEncryptKey("");
        $alipayConfig->setFormat("json");
        $alipayConfig->setAppCertPath($this->config['app_cert_path']);
        $alipayConfig->setAlipayPublicCertPath($this->config['alipay_cert_path']);
        $alipayConfig->setRootCertPath($this->config['alipay_root_cert_path']);
        $alipayClient = new AopCertClient($alipayConfig);
        $alipayClient->isCheckAlipayPublicCert = true;
        $request = new \AlipayFundTransUniTransferRequest();
        $bizContentarr = $data;
        $bizContent = json_encode($bizContentarr,JSON_UNESCAPED_UNICODE);
        $request->setBizContent($bizContent);
        $responseResult = $alipayClient->execute($request);
        $responseApiName = str_replace(".","_",$request->getApiMethodName())."_response";
        $response = $responseResult->$responseApiName;
        if(!empty($response->code)&&$response->code==10000){
            return $response;
        }
        else{
            return $response;
        }
    }

    /**
     * 转账查询接口
     * @param $data
     * "{".
    "\"out_biz_no\":\"201808080001\"
    "\"order_id\":\"20190619110070000006800000025890\"
    "\"biz_scene\":\"DIRECT_TRANSFER\"
    "\"pay_fund_order_id\":\"20190801110070001506380000251556\"
    "\"product_code\":\"TRANS_ACCOUNT_NO_PWD\"
    "}"
     * @return \SimpleXMLElement
     * @throws \Exception
     */
    public function search($data){
        $aopAlipayClient = new \AopCertClient;
        $appCertPath = $this->config['app_cert_path'];//"应用证书路径（要确保证书文件可读），例如：/home/admin/cert/appCertPublicKey.crt";
        $alipayCertPath = $this->config['alipay_cert_path'];//"支付宝公钥证书路径（要确保证书文件可读），例如：/home/admin/cert/alipayCertPublicKey_RSA2.crt";
        $rootCertPath = $this->config['alipay_root_cert_path'];//"支付宝根证书路径（要确保证书文件可读），例如：/home/admin/cert/alipayRootCert.crt";
        $aopAlipayClient->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aopAlipayClient->appId = $this->config['appId'];
        $aopAlipayClient->rsaPrivateKey = '请填写开发者私钥去头去尾去回车，一行字符串' ;
        $aopAlipayClient->format = "json";
        $aopAlipayClient->charset= "UTF-8";
        $aopAlipayClient->signType= "RSA2";
//调用getPublicKey从支付宝公钥证书中提取公钥
        $aopAlipayClient->alipayrsaPublicKey = $aopAlipayClient->getPublicKey($alipayCertPath);
//是否校验自动下载的支付宝公钥证书，如果开启校验要保证支付宝根证书在有效期内
        $aopAlipayClient->isCheckAlipayPublicCert = true;
//调用getCertSN获取证书序列号
        $aopAlipayClient->appCertSN = $aopAlipayClient->getCertSN($appCertPath);
//调用getRootCertSN获取支付宝根证书序列号
        $aopAlipayClient->alipayRootCertSN = $aopAlipayClient->getRootCertSN($rootCertPath);
        $request = new \AlipayFundTransCommonQueryRequest();
        $request->setBizContent(json_encode($data,JSON_UNESCAPED_UNICODE));
        $responseResult = $aopAlipayClient->execute($request);
        $responseApiName = str_replace(".","_",$request->getApiMethodName())."_response";
        $response = $responseResult->$responseApiName;
        if(!empty($response->code)&&$response->code==10000){
            return $response;
        }
        else{
            return $response;
        }
    }
}

